﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace TM_Server
{
    public partial class Form1 : Form
    {
        #region 构造函数部分

        public Form1()
        {
            InitializeComponent();
            //初始化状态显示
            this.Update_State();
            this.Stop_TM.Enabled = false;
        }

        #endregion

        #region 刷新 state 标签的字体还有颜色，内容

        /*
         * 此方法用于根据全局变量 Globle._isRuning
         * 来刷新 state 标签的字体还有颜色，内容
         */
        public void Update_State()
        {
            if (Globle._isRuning == 0)
            {
                this.state.BackColor = Color.Transparent;
                this.state.ForeColor = Color.Red;
                this.state.Text = "未启动...";
            }
            else
            {
                this.state.BackColor = Color.Transparent;
                this.state.ForeColor = Color.Green;
                this.state.Text = "已经启动，当前上线端口：" + Globle._OnLinePort;
            }
        }

        #endregion

        #region 启动服务器

        private void 启动TM服务器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //如果还没有启动服务端
            if (Globle._isRuning == 0)
            {
                Wait2Online Lis_Online = new Wait2Online(this);
                Globle.Wait2Online = Lis_Online;
                Globle.Main_f = this;
                Thread thread = new Thread(new ThreadStart(Lis_Online.Lis_Port));
                thread.Start();  //启动线程进行监听
            }
        }

        #endregion

        #region 刷窗体，退出

        /*
         * 此方法用于刷窗体
         */
        public void Update_TM_Run()
        {
            this.Run_TM.Enabled = false;
            this.Stop_TM.Enabled = true;
        }

        /*
         * 此方法用于刷窗体
         */
        public void Update_TM_Stop()
        {
            this.Run_TM.Enabled = true;
            this.Stop_TM.Enabled = false;
        }

        private void 关闭TM服务器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Globle.Wait2Online.Stop_Lis_Port();
        }

        private void 关闭程序XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();  //退出程序
        }

        #endregion

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}